# coding: utf-8
from sqlalchemy import Column, Integer, String, Text, DateTime
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
metadata = Base.metadata


class BaseModel:
    id = Column(Integer, primary_key=True, comment="主键ID")
    created_by = Column(String(20), nullable=True, comment="创建人")
    created_at = Column(DateTime, nullable=True, comment="创建时间")
    updated_by = Column(String(20), nullable=True, comment="修改人")
    updated_at = Column(DateTime, nullable=True, comment="修改时间")
    is_delete = Column(Integer, nullable=True, default=0, comment="是否删除 0-否 1-是")

    def to_dict(self):
        res = {c.name: getattr(self, c.name, None) for c in self.__table__.columns}
        return res


class ErrorLog(Base):
    """报警日志"""
    __tablename__ = 't_error_log'

    id = Column(Integer, primary_key=True, comment="主键ID")
    type = Column(String(255), nullable=True, comment="报警类型")
    message = Column(Text, nullable=True, comment="报警描述")
    suggestion = Column(Text, nullable=True, comment="建议操作")
    created_at = Column(DateTime, nullable=True, comment="报警时间")


class FND(Base, BaseModel):
    __tablename__ = 't_fnd'

    # 定义字段和备注
    gpc_fnd = Column(String(32), index=True, unique=True, comment='GPC/FND')
    model = Column(String(32), comment='车型')
    part_name = Column(String(512), comment='零件名称')
    qty = Column(Integer, comment='数量')
    gpc = Column(String(8), comment='GPC')
    fnd = Column(String(8), comment='FND')
    fnd_cn = Column(String(512), comment='FND中文')

    @classmethod
    def from_dict(cls, data_dict):
        instance = cls()
        for key, value in data_dict.items():
            if hasattr(instance, key):
                setattr(instance, key, value)
        return instance


class SapMaterialDetailsInfo(Base, BaseModel):
    __tablename__ = 't_sap_material_details_info'

    # 定义字段和备注
    vehicle_part_id = Column(Integer, comment='整车物料号表id')
    werks = Column(String(5), comment='工位')
    matnr = Column(String(16), comment='整车物料号')
    maktx1 = Column(String, comment='物料号')
    idnrk = Column(String, comment='idnrk')
    maktx2 = Column(String, comment='零件名称')
    menge = Column(String, comment='数量')
    text1 = Column(String, comment='GPC/FND')
    text2 = Column(String, comment='text2')
    text3 = Column(String, comment='text3')
    text4 = Column(String, comment='text4')
    text5 = Column(String, comment='text5')
    text6 = Column(String, comment='text6')

    @classmethod
    def from_dict(cls, data_dict):
        instance = cls()
        for key, value in data_dict.items():
            if hasattr(instance, key):
                setattr(instance, key, value)
        return instance


class DoorVehicle(Base, BaseModel):
    """门护板车辆信息"""
    __tablename__ = 't_door_vehicle'
    vin = Column(String(32), index=True, unique=True, comment='17位VIN号')
    vehicle_part = Column(String(32), comment='整车物料号')
    vehicle_part_id = Column(Integer, comment='整车物料号表id')
    door_key = Column(String(1024), comment='门类型Key')

    @classmethod
    def from_dict(cls, data_dict):
        instance = cls()
        for key, value in data_dict.items():
            if hasattr(instance, key):
                setattr(instance, key, value)
        return instance


class FloorVehicle(Base, BaseModel):
    """后底板车辆信息"""
    __tablename__ = 't_floor_vehicle'
    vin = Column(String(255), nullable=True, comment="vin")
    vinCode = Column(String(255), nullable=True, comment="vin")
    station = Column(String(255), nullable=True, comment="工位名称")
    model = Column(String(255), nullable=True, comment="车型")
    batchCode = Column(String(255), nullable=True, comment="批次号")
    lineName = Column(String(255), nullable=True, comment="产线代码")
    carMaterialCode = Column(String(255), nullable=True, comment="物料号")
    orderNum = Column(String(255), nullable=True, comment="订单号")
    frameNo = Column(String(255), nullable=True, comment="车身号")
    wmaterialDesc = Column(String(255), nullable=True, comment="白车身描述")
    wmaterialType = Column(String(255), nullable=True, comment="白车身车型")
    wmaterialCode = Column(String(255), nullable=True, comment="白车身号")
    status = Column(String(16), default='未检测', comment='检测状态：未检测，已检测，漏检')
    result = Column(String(6), nullable=True, comment="检测结果 OK NG ByPass")

    @classmethod
    def from_dict(cls, data_dict):
        """
        根据字典创建VehicleInfo对象。
        :param data_dict: 包含属性值的字典。
        :return: VehicleInfo类的实例。
        """
        instance = cls()
        for key, value in data_dict.items():
            if hasattr(instance, key):
                setattr(instance, key, value)
        return instance


class VehiclePart(Base, BaseModel):
    """整车物料号表"""
    __tablename__ = 't_vehicle_part'
    vehicle_part = Column(String(32), comment='整车物料号')
    door_key = Column(String(1024), index=True, unique=False, comment='门类型Key')

    @classmethod
    def from_dict(cls, data_dict):
        instance = cls()
        for key, value in data_dict.items():
            if hasattr(instance, key):
                setattr(instance, key, value)
        return instance
